package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 上传[模板文件]
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            List<String[]> excel = POIUtils.readExcel(excelFile);
            if (excel !=null && excel.size() > 0){
                List<OrderSetting> orderSettings = new ArrayList<>();
                //遍历每一行
                for (String[] hang : excel) {//2019-03-05 //200
                    //遍历每一列
                    Date orderDate = new Date(hang[0]);//2019-03-05
                    Integer number = new Integer(hang[1]);//200
                    OrderSetting orderSetting = new OrderSetting(orderDate, number);
                    orderSettings.add(orderSetting);
                }
                orderSettingService.upload(orderSettings);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    /**
     * 通过[年-月-]获取[OrderSetting部分数据集合]
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByYearAndMonth")
    public Result getOrderSettingByYearAndMonth(String date){//2020-12-
        try {
            //返回结果可以为空
            List<Map> res = orderSettingService.getOrderSettingByYearAndMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,res);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/updateOrderSettingNumberByInput")
    public Result updateOrderSettingNumberByInput(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.updateOrderSettingNumberByInput(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
